﻿using System;
using System.Windows.Forms;
using System.Xml;
using KTDSMS;

namespace KTD_XSMS
{
    public partial class AddPhone : Form
    {
        XmlDocument Doc = new XmlDocument();
        public string id;
        public AddPhone()
        {
            InitializeComponent();
        }
        public void Message(string mess)
        {
            MessageBox.Show(mess, "Thông báo hệ thống", MessageBoxButtons.OK);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string text = (string) null;
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
                text = "Please enter Name";
           
            if (string.IsNullOrEmpty(text))
            {
                if (string.IsNullOrEmpty(this.txtPhoneNo.Text.Trim()))
                    text = "Please enter PhoneNo";
            }
            if (Unity.Incontent(txtPhoneNo.Text.Trim()) == "")
            {
                Unity.Message("Format number incorect");
                return;
            }
            if (string.IsNullOrEmpty(text))
            {
                Doc.Load(Application.StartupPath + "\\PhoneTable.xml");
                XmlElement Phone_Table, Ten, SoDT,ID;
                Phone_Table = Doc.CreateElement("PhoneTable");
                Ten = Doc.CreateElement("Name");
                SoDT = Doc.CreateElement("PhoneNo");
                ID = Doc.CreateElement("ID");
                Ten.InnerText = txtName.Text.Trim();
                SoDT.InnerText = txtPhoneNo.Text.Trim();
                ID.InnerText = id;

                Phone_Table.AppendChild(Ten);
                Phone_Table.AppendChild(SoDT);
                Phone_Table.AppendChild(ID);

                Doc.DocumentElement.AppendChild(Phone_Table);
                Doc.Save(Application.StartupPath + "\\PhoneTable.xml");

                Message("AddPhone Success.");
                base.Dispose(); 
            }
            else
            {
                MessageBox.Show(text);
            }
        
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Dispose();
        }

        private void txtPhoneNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtName.Focus();
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSave.Focus();
            }
        }

        private void AddPhone_Load(object sender, EventArgs e)
        {
            txtPhoneNo.Focus();
        }
    }
}

